<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>tabbar</title>
  <script src="../../vue.js"></script>
  <style>
    * {
      box-sizing: border-box;
    }

    html, body {
      padding: 0;
      margin: 0;
      width: 100%;
      height: 100%;
    }

    ul, li {
      list-style: none;
      margin: 0;
      padding: 0
    }

    #root {
      height: 100%;
      display: flex;
      flex-direction: column;
    }

    main {
      flex: 1;
    }

    ul {
      height: 44px;
      display: flex;
      border-top: 1px solid #ccc;
    }

    ul > li {
      flex: 1;
      text-align: center;
      line-height: 44px;
    }

    ul > li.active {
      background: chocolate;
      color: #fff;
    }
  </style>
</head>
<body>
  <div id="root">
    <main>
      <component :is="componentId"></component>
    </main>
    <ul>
      <li 
        v-for="tab, index in tabItems"
        @click="handleClick(index, tab.component)"
        :class="{active: curIndex === index}"
        :key="tab.component"
      >
        {{tab.title}}
      </li>
    </ul>
  </div>
  <script>
    const tab1 = {
      template: `
        <div>page1</div>
      `,
      mounted() {
        console.log('mounted1.')
      },
    }
    const tab2 = {
      template: `
        <div>page2</div>
      `,
      mounted() {
        console.log('mounted2.')
      },
    }
    const tab3 = {
      template: `
        <div>page3</div>
      `,
      mounted() {
        console.log('mounted3.')
      },
    }

    var vm = new Vue({
      el: '#root',
      data: {
        curIndex: 0,
        componentId: 'tab1',
        tabItems: [
          {
            component: 'tab1',
            title: '首页'
          },
          {
            component: 'tab2',
            title: '搜索'
          },
          {
            component: 'tab3',
            title: '我的'
          }
        ]
      },
      methods: {
        handleClick(index, component) {
          this.curIndex = index
          this.componentId = component
        }
      },
      components: {
        tab1,
        tab2,
        tab3
      }
    })
  </script>
</body>
</html>